#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Create your views here.
from django.shortcuts import render_to_response
from winlog.log_dj.models import *
from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger
from django.db.models import Q
from winlog.log_dj.forms import *

def index(request):
    after_range_num = 5
    before_range_num = 4
    try:
        page=int(request.GET.get('page','1'))
        if page < 1:
            page=1
    except ValueError:
        page=1
    winlog_list = Winlog.objects.all().order_by('-id')
    paginator = Paginator(winlog_list, 10)
    try:
        winloglist = paginator.page(page)
    except (EmptyPage,InvalidPage,PageNotAnInteger):
        winloglist = paginator.page(1)
    if page >= after_range_num:
        page_range = paginator.page_range[page-after_range_num:page+before_range_num]
    else:
        page_range = paginator.page_range[0:int(page)+before_range_num]
    return render_to_response('log_dj/index.html', locals())


def search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(type__icontains=query)
        )
        results = Winlog.objects.filter(qset)
    else:
        results = []
    return render_to_response("log_dj/search.html", {
        "results": results,
        "query": query
    })
            
def search1(request):
    after_range_num = 5
    before_range_num = 4
    try:
        page=int(request.GET.get('page','1'))
        if page < 1:
            page=1
    except ValueError:
        page=1
    if request.method == 'GET':    
        form = LogSearchForm(request.GET)
        if form.is_valid():
            stardate = form.cleaned_data['stardate']
            startime = form.cleaned_data['startime']
            enddate = form.cleaned_data['enddate']
            endtime = form.cleaned_data['endtime']
            sourcename = form.cleaned_data['sourcename']
            categorystring = form.cleaned_data['categorystring']
            type = form.cleaned_data['type']
            eventcode = form.cleaned_data['eventcode']
            computername = form.cleaned_data['computername']
            users  = form.cleaned_data['users'] 
            
            print sourcename
            b = '&sourcename='.join(sourcename)
            print b
            results = Winlog.objects.filter(sourcename__in = sourcename, type__in = type)
#           results = Winlog.objects.filter(sourcename__in=sourcename,
#                                            categorystring__in=categorystring,
#                                            type__in=type,
#                                            eventcode__in=eventcode,
#                                            computername__in=computername,
#                                            users__in=users
#                                            )
            paginator = Paginator(results,10)
            try:
                result = paginator.page(page)
            except (EmptyPage,InvalidPage,PageNotAnInteger):
                result = paginator.page(1)
            if page >= after_range_num:
                page_range = paginator.page_range[page-after_range_num:page+before_range_num]
            else:
                page_range = paginator.page_range[0:int(page)+before_range_num]
            return render_to_response('log_dj/search1.html',locals())
    else:
        form = LogSearchForm()
    return render_to_response('log_dj/search1.html',locals())


def search2(request, filter=None):
    if request.method == 'GET':
        form = LogSearchForm(request.GET)
        if form.is_valid():
            after_range_num = 5
            before_range_num = 4
            try:
                page=int(request.GET.get('page','1'))
                if page < 1:
                    page=1
            except ValueError:
                page=1
            cd = form.cleaned_data
            t = cd.get('sourcename',[])
            results = Winlog.objects.filter(sourcename__in=t)
            paginator = Paginator(results,5)
            try:
                result = paginator.page(page)
            except (EmptyPage,InvalidPage,PageNotAnInteger):
                result = paginator.page(1)
            if page >= after_range_num:
                page_range = paginator.page_range[page-after_range_num:page+before_range_num]
            else:
                page_range = paginator.page_range[0:int(page)+before_range_num]
            queries_without_page = request.GET.copy()
            if queries_without_page.has_key('page'):
                del queries_without_page['page']
            return render_to_response('log_dj/search2.html',locals())
    
    results = queries_without_page 
    return render_to_response('log_dj/search1.html',locals())



def search3(request):
    after_range_num = 5
    before_range_num = 4
    try:
        page=int(request.GET.get('page','1'))
        if page < 1:
            page=1
    except ValueError:
        page=1
    if request.method == 'GET':    
        form = LogSearchForm1(request.GET)
        if form.is_valid():
            sourcename = form.cleaned_data['sourcename']
	    print sourcename
            b = '&sourcename='.join(sourcename)
            print b
            results = Winlog.objects.filter(sourcename__in = sourcename)
            paginator = Paginator(results,10)
            try:
                result = paginator.page(page)
            except (EmptyPage,InvalidPage,PageNotAnInteger):
                result = paginator.page(1)
            if page >= after_range_num:
                page_range = paginator.page_range[page-after_range_num:page+before_range_num]
            else:
                page_range = paginator.page_range[0:int(page)+before_range_num]
            return render_to_response('log_dj/search3.html',locals())
    else:
        return render_to_response('log_dj/search3.html',locals())

